<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['params']=array();
$return['status']=$this->order_status;
$return['buytype']=$this->buy_types;
$return['sendtype']=$this->send_types;
$return['send_status']=$this->send_status;
$return['paytype']=get_pay_types();
$return['paytype']['peerpay']='找人代付';
$return['ispay']=array('0'=>'未支付','1'=>'已支付');
$tabs=array(
	array('op'=>'list','title'=>'客户总表'),
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	if(!empty($_RQ['mid'])){
		$params['mid']=trim($_RQ['mid']);
        $return['params']['mid']=$_RQ['mid'];
	}else{
		$oparams=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'istaocan'=>1,'ispay'=>1,'status'=>array(1,2,3,10,11,12,13));
		$mids=pdo_getall('jy_weishop_order_goods',$oparams,'mid','mid');
		if($mids){
			$mids=array_keys($mids);
		}else{
			$mids=-1;
		}
		$params['mid']=$mids;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$members=pdo_getslice('jy_weishop_member',$params,array($pindex,$psize),$total,array('mid','merchid'),'',$orderby);
	$list=array();
	foreach($members as $mv){
		$member=cfc('core')->member_show($mv['mid']);
		$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mv['mid'],'merchid'=>$merchid,'istaocan'=>1,'ispay'=>1,'status'=>array(1,2,3,10,11,12,13));
		$order_goods=pdo_getall('jy_weishop_order_goods',$params);
		foreach($order_goods as $item){
				$goodskey2=$item['mid'].'_'.$item['goodsid'];
				$item['goodsnum']=$item['goodsnum']?:1;
				$item['total']=$item['total']?:1;
				$totalnum=$item['goodsnum']*$item['total'];
				if($list[$goodskey2]){
					$list[$goodskey2]['allnum'] +=$totalnum;
				}else{
					$g['member']=$member;
					$g['mid']=$item['mid'];
					$g['goodsid']=$item['goodsid'];
					$list[$goodskey2]=$g;
					$list[$goodskey2]['allnum']=$totalnum;
				}
		}
	}
	$list=array_values($list);
    foreach($list as &$lv){
        $lv['allprice']= pdo_getsum('jy_weishop_order_goods',array('goodsid'=>$lv['goodsid'],'mid'=>$lv['mid'],'merchid'=>$merchid),'total_price');
        $lv['issendnum']=$this->pdo_getsum('goodsset',array('goodsid'=>$lv['goodsid'],'mid'=>$lv['mid'],'merchid'=>$merchid),'issendnum');
        $lv['lastnum']=$lv['allnum']-$lv['issendnum'];
        $lv['title']= pdo_getcolumn('jy_weishop_goods',array('id'=>$lv['goodsid'],'uniacid'=>$_SESSION['uniacid']),'title');
        $lv['is_dingpei']=1;
        $lv['peisong_id']= pdo_getcolumn('jy_peimilk_orderuser',array('mid'=>$lv['mid'],'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'peisong_id');
    }
	$return['list']=$list;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：这个不分商品规格和类型，客户总数据管理(<red>这里显示会员所有订单商品数据，包括未产生配送记录的</red>)！','edit'=>false,'total'=>$total,'psize'=>$psize,'del_one'=>false,'delall'=>false,'list'=>array(
				array('name'=>'mid','title'=>'MID','type'=>'string'),
				array('name'=>'member','title'=>'会员信息','type'=>'member'),
				array('name'=>'title','title'=>'商品名称','type'=>'string'),
				array('name'=>'allprice','title'=>'总金额','type'=>'string'),
                array('name'=>'peisong_id','title'=>'配送员','type'=>'select','options'=>'peisongs','option_value'=>'mid'),
                array('name'=>'is_dingpei','title'=>'配送方式','type'=>'radio','options'=>'is_dingpei'),
				array('name'=>'allnum','title'=>'总配送','type'=>'string'),
				array('name'=>'issendnum','title'=>'已配送','type'=>'string'),
                array('name'=>'lastnum','title'=>'待配送','type'=>'string'),
		),'action'=>array(
			//array('type'=>'href','key'=>'mid','action'=>'jy_peimilk/merch.sendlog_pc/','title'=>'查看配送详情'),
			array('type'=>'post','key'=>'mid','op'=>'update','title'=>'刷新待配送单'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'title'=>'会员ID','name'=>'mid','action'=>'jy_merch/get_member/'),
		),
	);
	$return['htmls']=$htmls;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
    $return['peisongs']=$this->getall('member',array('uniacid'=>$_SESSION['uniacid']),array('mid','title'),'mid');
    $return['is_dingpei']=array('一次性配送','定期配送');
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'plugin_name <>'=>'jy_mfuwu');
	$params['deleted']=intval($_RQ['deleted']);
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(isset($_RQ['status']) && $_RQ['status'] !='100'){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}else{
		$return['params']['status']=100;
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	$order=pdo_getall('jy_weishop_order',$params);
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('wechat'=>'微信支付','credit'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	foreach($order as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');		
		$address=$v['address']?json_decode($v['address'],true):$v['addressid'];
		if($address){
			$v['address']=model('core/member')->get_address_str($address);
		}
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['realname']?:$address['name'];
		$v['status']=$status[$v['status']];
		$v['paytype']=$paytype[$v['paytype']];
		$v['ispay']=$ispay[$v['ispay']];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$v['storeid']),'title');
		$v['storetitle']=$v['storetitle']?:'无';
		$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id']),array('goodsid','optionid'));
		foreach($goods as $k=>$gv){
			$goodstitle=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['goodsid']),'title');
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$selespename=implode("，",$specs);
			}else{
				$selespename='无规格';
			}
			$num=$k+1;
			$v['goodstitle'] .='【商品'.$num.'】['.$selespename.']'.$goodstitle;
		}
	}
	$header=array(
		'ordersn'=>'订单号',
		'nickname'=>'买家昵称',
		'price'=>'金额',
		'status'=>'订单状态',
		'ispay'=>'金额',
		'paytype'=>'订单状态',
		'realname'=>'收货人姓名',
		'address'=>'收货地址',
		'mobile'=>'收货人电话',
	);
	cfc('tools')->export2excel($header,$order,'订单');
}
if($_RQ['op']=='update'){
	$mid=$_RQ['mid'];
	if(!$mid){
		exi('请选择会员！','error');
	}
	$allgoods=$this->model('order')->get_allgoods($mid,$merchid);
	foreach($allgoods as $k=>$av){
		$this->model('model')->check_goodsset($av);
	}
	$yuesetting=$this->plugin_setting('stopsetting'.$merchid);
	$this->model('order')->upstopdays($yuesetting['nopei_days'],$mid,$merchid);
	exi('配置成功！');
}
?>